package com.hqyj.hrms.modules.recruitment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.hrms.modules.recruitment.dao.ApplyMapper;
import com.hqyj.hrms.modules.recruitment.entity.Apply;
import com.hqyj.hrms.modules.recruitment.service.IApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.hrms.utils.MyEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.Record;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-09-26
 */
@Service
public class ApplyServiceImpl implements IApplyService {
    @Autowired(required = false)
    ApplyMapper applyMapper;

    //注入邮件发送工具类
    @Autowired
    MyEmail myEmail;

    @Override
    public HashMap<String, Object> applyList(Apply apply) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        String keyWord = apply.getKeyWord();
        QueryWrapper wrapper = new QueryWrapper<Record>()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "apply_id", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "apply_name", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "apply_sex", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "apply_bir", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "apply_phone", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "apply_education", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "apply_email", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "apply_job", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "apply_date", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "apply_status", keyWord
                );
        Page<Apply> salaryPage = new Page<>(apply.getPage(), apply.getRow());
        //第一步：创建分页对象
        Page<Apply> applyPage = new Page<>(apply.getPage(), apply.getRow());
        //第二步：查询分页，获取分页数据集
        Page<Apply> applyPageList = applyMapper.selectPage(applyPage, wrapper);
        //获取当前页的集合
        map.put("info", applyPageList.getRecords());
        //获取总条数
        map.put("total", applyPageList.getTotal());
        //获取总页数
        map.put("totalPage", applyPageList.getPages());
        return map;
    }

    @Override
    public HashMap<String, Object> applyAdd(Apply apply) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = applyMapper.insert(apply);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> applyUpdate(Apply apply) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = applyMapper.updateById(apply);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> applyDel(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = applyMapper.deleteById(id);
        if (num > 0) {
            map.put("info", "删除成功");
        } else {
            map.put("info", "删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> applyList01(Apply apply) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<>();
        params.put("apply_status", "未处理");
        QueryWrapper<Apply> wrapper = new QueryWrapper<>();
        wrapper.allEq(params);
        Page<Apply> applyPageList = new Page<>(apply.getPage(), apply.getRow());
        Page<Apply> pageList = applyMapper.selectPage(applyPageList, wrapper);
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
        return map;

    }

    @Override
    public HashMap<String, Object> applyList02(Apply apply) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<>();
        params.put("apply_status", "简历通过");
        QueryWrapper<Apply> wrapper = new QueryWrapper<>();
        wrapper.allEq(params);
        Page<Apply> applyPageList = new Page<>(apply.getPage(), apply.getRow());
        Page<Apply> pageList = applyMapper.selectPage(applyPageList, wrapper);
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
        return map;
    }

    @Override
    public HashMap<String, Object> applyList03(Apply apply) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<>();
        params.put("apply_status", "笔试通过");
        QueryWrapper<Apply> wrapper = new QueryWrapper<>();
        wrapper.allEq(params);
        Page<Apply> applyPageList = new Page<>(apply.getPage(), apply.getRow());
        Page<Apply> pageList = applyMapper.selectPage(applyPageList, wrapper);
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
        return map;
    }

    @Override
    public HashMap<String, Object> applyList04(Apply apply) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<>();
        params.put("apply_status", "面试通过");
        QueryWrapper<Apply> wrapper = new QueryWrapper<>();
        wrapper.allEq(params);
        Page<Apply> applyPageList = new Page<>(apply.getPage(), apply.getRow());
        Page<Apply> pageList = applyMapper.selectPage(applyPageList, wrapper);
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
        return map;
    }

    @Override
    public HashMap<String, Object> applyList05(Apply apply) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<>();
        params.put("apply_status", "招聘通过");
        QueryWrapper<Apply> wrapper = new QueryWrapper<>();
        wrapper.allEq(params);
        Page<Apply> applyPageList = new Page<>(apply.getPage(), apply.getRow());
        Page<Apply> pageList = applyMapper.selectPage(applyPageList, wrapper);
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
        return map;
    }

    @Override
    public HashMap<String, Object> applyList06(Apply apply) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<>();
        params.put("apply_status", "人才库");
        QueryWrapper<Apply> wrapper = new QueryWrapper<>();
        wrapper.allEq(params);
        Page<Apply> applyPageList = new Page<>(apply.getPage(), apply.getRow());
        Page<Apply> pageList = applyMapper.selectPage(applyPageList, wrapper);
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
        return map;
    }

    @Override
    public HashMap<String, Object> emailSend(String email, String message,HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map = myEmail.sendEmail(email, message, "xxxx公司的邮件");
        if (map.get("info").equals("发送成功")) {
            HttpSession session = request.getSession();
            session.setAttribute("message", message);
        }
        return map;
    }


}
